import 'package:animations/animations.dart';
import 'package:cat_eye/base/base_refresh_state.dart';
import 'package:cat_eye/bean/home_body_bean.dart';
import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/util/util.dart';
import 'package:cat_eye/view_model/find_view_model/find_info_view_model.dart';
import 'package:cat_eye/web_view/web_view_widget.dart';
import 'package:flutter/material.dart';

/*
* @Package: root_page.find_page
* @ClassName: find_info_widget
* @Author: szj
* @CreateDate: 8/4/21 5:54 PM
* TODO 发现-资讯页面
*/
class FindInfoWidget extends StatefulWidget {
  const FindInfoWidget({Key key}) : super(key: key);

  @override
  _FindInfoWidgetState createState() => _FindInfoWidgetState();
}

class _FindInfoWidgetState
    extends BaseRefreshState<FindInfoWidget, FindInfoViewModel> {
  @override
  FindInfoViewModel get buildViewModel => FindInfoViewModel();

  @override
  Widget buildWidget(BuildContext context, FindInfoViewModel model) {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        if (model.itemList[index].type == StringConfig.TEXT_CARD) {
          return _text(model.itemList[index]);
        } else if (model.itemList[index].type ==
            StringConfig.INFO_RMATION_CARD) {
          return otherLayout(model.itemList[index], context);
        }

        return Text("");
      },
      itemCount: model.itemList.length,
    );
  }

  Widget _text(ItemList itemList) {
    return Padding(
        padding: EdgeInsets.only(top: 5, bottom: 5, left: 10),
        child: text(itemList.data.text,
            fontWeight: FontWeight.bold, fontSize: 22));
  }

  Widget otherLayout(ItemList itemList, BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(8),
        child: Column(
          children: [
            OpenContainer(
              openBuilder: (BuildContext context,
                  void Function({Object returnValue}) action) {
                return _webView(itemList);
              },
              closedBuilder: (BuildContext context, void Function() action) {
                return cacheImage(itemList.data.backgroundImage,
                    width: MediaQuery.of(context).size.width, height: 160);
              },
            ),
            Container(
              color: Color(0xFFEDEDED),
              width: MediaQuery.of(context).size.width,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: itemList.data.titleList
                    .map((e) => Padding(
                        padding: EdgeInsets.only(top: 3, left: 5, bottom: 3),
                        child: text(e)))
                    .toList(),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _webView(ItemList itemList) {
    /**
     *  第一步: 请求出来 actionUrl 是这样的 【接口中的出现的%3a%2f%2 是// 】
     *  "actionUrl": "eyepetizer://webview/?title=%e6%af%8f%e6%97%a5%e6%96%b0%e9%b2%9c%e8%b5%84%e8%ae%af&
     *         url=https%3a%2f%2fwww.kaiyanapp.com%2fnew_article.html%3fnid%3d2617%26shareable%3dtrue%26rid%3d947"
     *
     *  第二步: 通过Uri.decodeComponent(String) 转化为
     *        url=https://www.kaiyanapp.com/new_article.html?nid=2617&shareable=true&rid=947
     */
    String url = Uri.decodeComponent(itemList.data.actionUrl
        .substring(itemList.data.actionUrl.indexOf("url")));

    /**
     *  第三步: 截取Url
     *   最终结果为:[https://www.kaiyanapp.com/new_article.html?nid=2617&shareable=true&rid=947]
     */
    url = url.substring(4, url.length);

    return WebViewWidget(
      url: url,
    );
  }
}
